package com.example.botany_api.controller;

import com.alibaba.fastjson.JSON;
import com.example.botany_api.bean.User;
import com.example.botany_api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 漫画忧作
 * @Date 2022/9/24 10:50
 * @Version 1.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //    登录接口
    @RequestMapping("/register")
    public String register(@RequestBody User user) {
        Map res = new HashMap();
        User data = userService.register(user);
        if (data != null) {
            res.put("user", data);
            return JSON.toJSONString(res);
        }
        return JSON.toJSONString(res);
    }

    //    管理员 获取用户数据接口
    @RequestMapping("/admin/userList")
    public String getUserList(@RequestBody HashMap req) {
        System.out.println(req.get("name"));
        String name = req.get("name").toString();
        String address = req.get("address").toString();
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", userService.getUserList(name, address, (current - 1) * size, size));
        res.put("total", userService.getUserListTotal(name, address));

        return JSON.toJSONString(res);
    }

    //    管理员 删除用户数据
    @RequestMapping("/admin/dl_User")
    public String dl_User(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = userService.dl_User(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
    //    管理员禁用或解禁
    @RequestMapping("/admin/di_User")
    public String di_User(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int state = Integer.parseInt(req.get("state").toString());
        if (state == 1) {
            state = 2;
        } else {
            state = 1;
        }
        int i = userService.di_User(id, state);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
    //    管理员 添加用户数据
    @RequestMapping("/admin/add_User")
    public String add_User(@RequestBody User user) {
        int i = userService.add_User(user);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 修改用户数据
    @RequestMapping("/admin/up_User")
    public String up_User(@RequestBody User user) {
        int i = userService.up_User(user);
        if (i==1){
            return "ok";
        }
        return "err";
    }
    //    管理员 修改管理员密码
    @RequestMapping("/admin/up_adminPass")
    public String up_adminPass(@RequestBody User user) {
        int i = userService.up_adminPass(user);
        if (i==1){
            return "ok";
        }
        return "err";
    }
//    图表接口

    @RequestMapping("/waterChart")
    public String waterChart() {
        Map res = new HashMap();
        int i=userService.waterChartCollect();
        int j=userService.waterChartViews();
        int arr[] = {i, j};
        res.put("dataList",arr);

        return JSON.toJSONString(res);
    }

    @RequestMapping("/topCount")
    public String topCount() {
        Map res = new HashMap();
        res.put("dataList",userService.topCount());

        return JSON.toJSONString(res);
    }
    @RequestMapping("/topChart")
    public String topChart() {
        Map res = new HashMap();
        res.put("views",userService.topChartViews());
        res.put("collect",userService.topChartCollect());
        return JSON.toJSONString(res);
    }
}

